package org.at.nhatky1;

import org.at.diary.user.ForgotPassword;
import org.at.diary.user.RegisterUser;
import org.at.diary.user.UserDatabaseAdapter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.YuvImage;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Login extends Activity {
    /** Called when the activity is first created. */
    public UserDatabaseAdapter userDbHelper;
	
	LinearLayout ll;
	private TextView tvNameDairy1;
	private TextView tvNameDairy2;
	private EditText edtUsername;
	private EditText edtPassLogin;
	private Button btnLogin;
	private Button btnRegister;
	private String password_from_typing;
	private String Password_from_database;
	private String username_from_type;
	private String username_from_database;
	
	private static final int FORGOT_PASSWORD = Menu.FIRST + 1;
	private static final int SETTING = Menu.NONE;
	private static final int ABOUT = Menu.FIRST +3;
	private static final int HELP = Menu.FIRST + 4;
	private static final int EXIT = Menu.FIRST + 5;
	private static final int CHANGE_BACKGROUND = Menu.FIRST + 6;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.login);
        userDbHelper = new UserDatabaseAdapter(Login.this);
        userDbHelper.open();
       
        tvNameDairy1 = (TextView) findViewById(R.id.diaryName);
        tvNameDairy2 = (TextView) findViewById(R.id.username);
        btnLogin = (Button) findViewById(R.id.btnLogin);
        btnRegister = (Button) findViewById(R.id.btnRegister);
        
        edtUsername = (EditText) findViewById(R.id.edtUsername);
        edtPassLogin = (EditText) findViewById(R.id.edtPassword);
        
        Cursor c = userDbHelper.fetchAllNotes();
        int i = c.getCount();
        
        if (i == 0){
        	tvNameDairy1.setText(R.string.nameDairy_sample);
        	tvNameDairy2.setText(R.string.fullname_sample);
        	Password_from_database = "1234";
        }
        
        else if (i != 0){
        	
        	Cursor userCursor = userDbHelper.fetchNote(1);
    		startManagingCursor(userCursor);
    		String diaryname = userCursor.getString(userCursor.getColumnIndexOrThrow(UserDatabaseAdapter.KEY_DIARYNAME));
    		String fullname = userCursor.getString(userCursor.getColumnIndexOrThrow(UserDatabaseAdapter.KEY_FULLNAME));
    		if (diaryname == null) {
    			tvNameDairy1.setText(R.string.nameDairy_sample);
    		}
    		else {
    			tvNameDairy1.setText(diaryname);
    		}
    		
    		if(fullname == null){
    			tvNameDairy2.setText(R.string.fullname_sample);
    		}
    		else {
    			tvNameDairy2.setText(fullname);
    		}
    		
        	Password_from_database = userCursor.getString(userCursor.getColumnIndexOrThrow(UserDatabaseAdapter.KEY_PASSWORD));
        	username_from_database = userCursor.getString(userCursor.getColumnIndexOrThrow(UserDatabaseAdapter.KEY_USERNAME));
        } 
        
        btnLogin.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				password_from_typing = edtPassLogin.getText().toString();		
				username_from_type = edtUsername.getText().toString();
				
				if (Password_from_database.equals(password_from_typing) && username_from_database.equals(username_from_type)){
					edtPassLogin.setText("");
					startActivity(new Intent(Login.this, Overview.class));
					userDbHelper.close();
				}
				else{
					edtPassLogin.setText("");
					
					Toast.makeText(Login.this, "Thông tin tài khoản không đúng. Xin vui lòng nhập lại.", Toast.LENGTH_LONG).show();
					
				}
			}
		});
        
        btnRegister.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(Login.this, RegisterUser.class);
				startActivity(i);
			}
		});
        
        
    }
	
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuItem item_change_pass = menu.add(Menu.NONE, FORGOT_PASSWORD, Menu.NONE, R.string.forgot_pass);
		item_change_pass.setIcon(R.drawable.forgotpassword);
		MenuItem item_setting = menu.add(Menu.NONE, SETTING, Menu.NONE, R.string.setting);
		item_setting.setIcon(android.R.drawable.ic_menu_preferences);
		MenuItem item_about = menu.add(Menu.NONE, ABOUT, Menu.NONE, R.string.about);
		item_about.setIcon(android.R.drawable.ic_menu_info_details);
		MenuItem item_help = menu.add(Menu.NONE, HELP, Menu.NONE, R.string.help);
		item_help.setIcon(android.R.drawable.ic_menu_help);
		MenuItem item_exit = menu.add(Menu.NONE, EXIT, Menu.NONE, R.string.exit);
		item_exit.setIcon(android.R.drawable.ic_lock_power_off);
		MenuItem item_change_background = menu.add(Menu.NONE, CHANGE_BACKGROUND, Menu.NONE, R.string.change_background);
		item_change_background.setIcon(android.R.drawable.menuitem_background);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case FORGOT_PASSWORD:
			Intent forgotPasswordIntent = new Intent(Login.this, ForgotPassword.class );
			startActivity(forgotPasswordIntent);
			return true;
		case SETTING:
			return true;
		case ABOUT:
			Dialog d = new Dialog(Login.this);
			d.setTitle(R.string.app_name);
			d.setContentView(R.layout.about);
			
			d.show();
			
			return true;
		case HELP:
			return true;
		case CHANGE_BACKGROUND:
			ll = (LinearLayout) findViewById(R.id.layout_login);
			
			
			
			return true;
		case EXIT:
			finish();
			return true;
		default:
			return false;
		}
		
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
	
	}
	
	
}